package com.yuke.cloud.service.pmc.service.impl;

import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.api.pmc.dto.RegionInfoDto;
import com.yuke.cloud.service.pmc.dao.ProdRegionIntroduceMapper;
import com.yuke.cloud.service.pmc.dto.ProdRegionInfoDto;
import com.yuke.cloud.service.pmc.dto.ProdRegionIntroduceDto;
import com.yuke.cloud.service.pmc.dto.ProdRegionRecommendDto;
import com.yuke.cloud.service.pmc.entity.*;
import com.yuke.cloud.service.pmc.service.*;
import com.yuke.cloud.common.core.support.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zys on 2018/09/30.
 */
@Slf4j
@Service
@Transactional
public class ProdRegionIntroduceServiceImpl extends BaseService<ProdRegionIntroduce> implements ProdRegionIntroduceService {
    @Resource
    private ProdRegionIntroduceMapper prodRegionIntroduceMapper;
    @Resource
    private ProdRegionPictureService prodRegionPictureService;
    @Resource
    private ProdRegionResortIntroduceService prodRegionResortIntroduceService;
    @Resource
    private ProdRegionResortPictureService prodRegionResortPictureService;
    @Resource
    private DicRegionService dicRegionService;
    @Resource
    private OpcFileFeignApi opcFileFeignApi;

    @Override
    public int save(ProdRegionIntroduce record) {
        record.setRegionIntroduceId(generateId());
        return super.save(record);
    }

    @Override
    public int update(ProdRegionIntroduce entity) {
        int count = super.update(entity);
        if (entity.getIsRecommend()!=null && !"".equals(entity.getIsRecommend())){
            ProdRegionResortIntroduce prodRegionResortIntroduce = new ProdRegionResortIntroduce();
            prodRegionResortIntroduce.setRegionId(entity.getRegionId());
            prodRegionResortIntroduce.setIsRecommend(entity.getIsRecommend());
            Example example = new Example(ProdRegionResortIntroduce.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("regionId", entity.getRegionId());
            prodRegionResortIntroduceService.updateByExample(prodRegionResortIntroduce, example);
        }
        if (null != entity.getProdRegionPictures() && entity.getProdRegionPictures().size() > 0) {
            List<ProdRegionPicture> prodRegionPictures = entity.getProdRegionPictures();
            UploadFile uploadFile = new UploadFile();
            List<String> filePaths = new ArrayList<>();
            for (ProdRegionPicture prodRegionPicture : prodRegionPictures) {
                if (prodRegionPicture.getRegionPictureId() == null) {
                    //新增
                    prodRegionPictureService.save(prodRegionPicture);
                } else{
                    if (prodRegionPicture.getOldBigPicUri() != null && prodRegionPicture.getOldSmallPicUri() != null) {
                        //删除
                        prodRegionPictureService.deleteById(prodRegionPicture.getRegionPictureId());
                        if (prodRegionPicture.getOldSmallPicUri() != null && !"".equals(prodRegionPicture.getOldSmallPicUri()))
                            filePaths.add(prodRegionPicture.getOldSmallPicUri());
                        if (prodRegionPicture.getOldBigPicUri() != null && !"".equals(prodRegionPicture.getOldBigPicUri()))
                            filePaths.add(prodRegionPicture.getOldBigPicUri());
                    }

                    // add by wg 20190515 更新图片记录
                    if (prodRegionPicture.getOldBigPicUri() == null && prodRegionPicture.getOldSmallPicUri() == null) {
                        Example example = new Example(ProdRegionPicture.class);
                        Example.Criteria criteria = example.createCriteria();
                        criteria.andEqualTo("regionPictureId", prodRegionPicture.getRegionPictureId());
                        prodRegionPictureService.updateByExample(prodRegionPicture, example);
                    }
                }
            }
            uploadFile.setFilePaths(filePaths);
            opcFileFeignApi.filesDelete(uploadFile);
        }
        return count;
    }

    @Override
    public void deleteByIds(String ids) {
        List<String> introduceIds = Arrays.asList(ids.split(","));
        List<Long> regionIds = this.getRegionsByIds(introduceIds.stream().map(id -> Long.valueOf(id)).collect(Collectors.toList()));

        super.deleteByIds(ids);

        List<ProdRegionPicture> prodRegionPictureList = prodRegionPictureService.getProdRegionPictureByRegionIds(regionIds);
        prodRegionPictureService.deleteByRegionIds(regionIds);
        prodRegionResortIntroduceService.deleteByRegionIds(regionIds);
        List<ProdRegionResortPicture> prodRegionResortPictureList = prodRegionResortPictureService.getProdRegionResortPictureByRegionIds(regionIds);
        prodRegionResortPictureService.deleteByRegionIds(regionIds);
        List<String> pictures = new ArrayList<>();
        if (prodRegionPictureList != null && prodRegionPictureList.size() > 0) {
            pictures.addAll(prodRegionPictureList.parallelStream().map(ProdRegionPicture::getBigPicUri).collect(Collectors.toList()));
            pictures.addAll(prodRegionPictureList.parallelStream().map(ProdRegionPicture::getSmallPicUri).collect(Collectors.toList()));
        }
        if (prodRegionResortPictureList != null && prodRegionResortPictureList.size() > 0) {
            pictures.addAll(prodRegionResortPictureList.parallelStream().map(ProdRegionResortPicture::getBigPicUri).collect(Collectors.toList()));
            pictures.addAll(prodRegionResortPictureList.parallelStream().map(ProdRegionResortPicture::getSmallPicUri).collect(Collectors.toList()));
        }
        if (pictures != null && pictures.size() > 0) {
            List<UploadFile> uploadFiles = new ArrayList<>();
            pictures.forEach(picture -> {
                UploadFile uploadFile = new UploadFile();
                uploadFile.setFilePath(picture);
                uploadFiles.add(uploadFile);
            });
            opcFileFeignApi.fileDelete(uploadFiles);
        }
//        prodRegionPictureList.forEach(prodRegionPicture -> {
//            UploadFile uploadFileBig = new UploadFile();
//            uploadFileBig.setFilePath(prodRegionPicture.getBigPicUri());
//            UploadFile uploadFileSmall = new UploadFile();
//            uploadFileSmall.setFilePath(prodRegionPicture.getSmallPicUri());
//            uploadFiles.add(uploadFileBig);
//            uploadFiles.add(uploadFileSmall);
//        });
//        prodRegionResortPictureList.forEach(prodRegionResortPicture -> {
//            UploadFile uploadFileBig = new UploadFile();
//            uploadFileBig.setFilePath(prodRegionResortPicture.getBigPicUri());
//            UploadFile uploadFileSmall = new UploadFile();
//            uploadFileSmall.setFilePath(prodRegionResortPicture.getSmallPicUri());
//            uploadFiles.add(uploadFileBig);
//            uploadFiles.add(uploadFileSmall);
//        });
    }

    @Override
    public List<ProdRegionRecommendDto> getAppDetailByCondition(ProdRegionIntroduce prodRegionIntroduce) {
        List<ProdRegionRecommendDto> prodRegionRecommendDtos = prodRegionIntroduceMapper.getAppDetailByCondition(prodRegionIntroduce);
        for (ProdRegionRecommendDto prodRegionRecommendDto : prodRegionRecommendDtos) {
            List<Long> regionIds = new ArrayList<>();
            regionIds.add(prodRegionRecommendDto.getRegionId());
            prodRegionRecommendDto.setProdPictures(prodRegionPictureService.getProdRegionPictureByRegionIds(regionIds));
        }
        return prodRegionRecommendDtos;
    }

    @Override
    public ProdRegionInfoDto detailByCityId(Long regionId) {
        ProdRegionInfoDto prodRegionInfoDto = new ProdRegionInfoDto();
        prodRegionInfoDto.setProdRegionIntroduce(prodRegionIntroduceMapper.getDetailByRegionId(regionId));
        prodRegionInfoDto.setPictures(prodRegionPictureService.getPicturesByRegionId(regionId));
        return prodRegionInfoDto;
    }

    @Override
    public List<ProdRegionIntroduceDto> getListDetailByCondition(ProdRegionIntroduce prodRegionIntroduce) {
        List<Long> regionIds = new ArrayList<>();
        Integer isRecommend = null;
        if (prodRegionIntroduce != null) {
            if (null != prodRegionIntroduce.getRegionId() && !"".equals(prodRegionIntroduce.getRegionId())) {
                DicRegion dicRegion = dicRegionService.selectByKey(prodRegionIntroduce.getRegionId());
                if (dicRegion.getLevelType() == 2) {
                    DicRegion d = new DicRegion();
                    d.setParentId(dicRegion.getRegionId());
                    List<DicRegion> dicRegions = dicRegionService.select(d);
                    regionIds = dicRegions.stream().map(dicRegion1 -> dicRegion1.getRegionId()).collect(Collectors.toList());
                }
                regionIds.add(prodRegionIntroduce.getRegionId());
            }

            if (null != prodRegionIntroduce.getIsRecommend() && !"".equals(prodRegionIntroduce.getIsRecommend())) {
                isRecommend = prodRegionIntroduce.getIsRecommend() ? 1 : 0;
            }
        }
        return prodRegionIntroduceMapper.getListDetailByCondition(regionIds, isRecommend);
    }

    @Override
    public List<ProdRegionIntroduce> getListByCondition(ProdRegionIntroduce prodRegionIntroduce) {
        return prodRegionIntroduceMapper.getListByCondition(prodRegionIntroduce);
    }

    @Override
    public List<Long> getRegionsByIds(List<Long> ids) {
        return prodRegionIntroduceMapper.getRegionsByIds(ids);
    }

    @Override
    public List<RegionInfoDto> getRegionIntroduceInfoByIds(List<Long> regionIds) {
        List<RegionInfoDto> regionInfoDtos = new ArrayList<>();
        if (regionIds == null || regionIds.size() == 0) {
            return regionInfoDtos;
        }
        return prodRegionIntroduceMapper.getRegionIntroduceInfoByIds(regionIds);
    }
}
